﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.model.Instance;
using MySql.Data.MySqlClient;
using l1cs.server.templates;
using System.Data;

namespace l1cs.server.datatables
{
    /// <summary>
    /// 掲示板クラス
    /// </summary>
    public class BoardTable
    {
        private static Logger _log = Logger.getLogger( typeof( BoardTable ).FullName );

        private static BoardTable _instance;

        private BoardTable() { }

        public static BoardTable getInstance()
        {
            if ( _instance == null )
            {
                _instance = new BoardTable();
            }
            return _instance;
        }

        /// <summary>
        /// DBに掲示板のトピックスを追加する。
        /// </summary>
        /// <param name="pc">プレイヤー</param>
        /// <param name="board">掲示内容</param>
        public void writeTopic( L1PcInstance pc, L1Board board )
        {/*TTOK*/
            lock ( this )
            {
                int count = 0;

                String sqlstr = "SELECT max(id)+1 FROM board ORDER BY id DESC";
                using ( MySqlWrapper wrapper = new MySqlWrapper() )
                {
                    Object obj = wrapper.ExecScaler( sqlstr );
                    if ( obj != null && !( obj is DBNull ) )
                    {
                        count = Convert.ToInt32( obj );
                    }
                    sqlstr = @"INSERT INTO board SET 
                     id     = ?1, 
                     name   = ?2, 
                     date   = ?3, 
                     title  = ?4, 
                     content= ?5";
                    MySqlCommand cmd = new MySqlCommand( sqlstr );
                    cmd.Parameters.Add( "?1", MySqlDbType.Int32 ).Value = count;
                    cmd.Parameters.Add( "?2", MySqlDbType.String ).Value = pc.getName();
                    cmd.Parameters.Add( "?3", MySqlDbType.String ).Value = board.date;
                    cmd.Parameters.Add( "?4", MySqlDbType.String ).Value = board.title;
                    cmd.Parameters.Add( "?5", MySqlDbType.String ).Value = board.content;
                    wrapper.ExecNonQuery( cmd );
                }
            }
        }

        /// <summary>
        /// 掲示板から該当のトピックスを削除する。
        /// </summary>
        /// <param name="number">トピックス番号</param>
        public void deleteTopic( int number )
        {/*TTOK*/
            String sqlstr = "DELETE FROM board WHERE id=?1";
            using ( MySqlWrapper wrapper = new MySqlWrapper() )
            {
                MySqlCommand cmd = new MySqlCommand( sqlstr );
                cmd.Parameters.Add( "?1", MySqlDbType.Int32 ).Value = number;
                wrapper.ExecNonQuery( cmd );
            }
        }

        /// <summary>
        /// 指定されたトピック番号から掲示内容を読み出す。
        /// </summary>
        /// <param name="number">トピック番号</param>
        /// <returns>掲示内容</returns>
        public L1Board getBoard( int number )
        {/*TTOK*/
            L1Board board = new L1Board();
            String sqlstr = "SELECT * FROM board WHERE id=?1";
            using ( MySqlWrapper wrapper = new MySqlWrapper() )
            {
                MySqlCommand cmd = new MySqlCommand( sqlstr );
                cmd.Parameters.Add( "?1", MySqlDbType.Int32 ).Value = number;
                DataTable dt = wrapper.GetDataTable( cmd );
                if ( dt.Rows.Count == 1 )
                {
                    board.id = Convert.ToInt32( dt.Rows[0]["id"] );
                    board.name = Convert.ToString( dt.Rows[0]["name"] );
                    board.date = Convert.ToString( dt.Rows[0]["date"] );
                    board.title = Convert.ToString( dt.Rows[0]["title"] );
                    board.content = Convert.ToString( dt.Rows[0]["content"] );
                }
            }
            return board;
        }

        /// <summary>
        /// 指定された番号からの掲示板一覧を表示する。
        /// </summary>
        /// <param name="start">開始番号</param>
        /// <returns>掲示板一覧</returns>
        public L1Board[] getViewList( int start )
        {/*TTOK*/
            L1Board[] boards;
            String sqlstr = @"SELECT * FROM board 
                                order by id desc
                                limit 8
                                offset ?1";
            using ( MySqlWrapper wrapper = new MySqlWrapper() )
            {
                MySqlCommand cmd = new MySqlCommand( sqlstr );
                cmd.Parameters.Add( "?1", MySqlDbType.Int32 ).Value = start;
                DataTable dt = wrapper.GetDataTable( cmd );
                boards = new L1Board[dt.Rows.Count];
                int i = 0;
                foreach ( DataRow dr in dt.Rows )
                {
                    boards[i].id = Convert.ToInt32( dr["id"] );
                    boards[i].name = Convert.ToString( dr["name"] );
                    boards[i].date = Convert.ToString( dr["date"] );
                    boards[i].title = Convert.ToString( dr["title"] );
                    boards[i].content = Convert.ToString( dr["content"] );
                    i++;
                }
            }
            return boards;
        }
    }
}
